<template>
  <!-- 自定义导航栏 -->
  <custom-navbar />

  <!-- 滚动容器 -->
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    refresher-enabled
    @refresherrefresh="onRefresh"
    :refresher-triggered="refreshing"
  >
    <!-- 骨架屏 -->
    <page-skeleton v-if="loading" />

    <template v-else>
      <!-- 轮播图 -->
      <XtxSwiper :bannerList="bannerList" />
      <!-- 分类 -->
      <category-panel :categoryList="categoryList" />
      <!-- 热门推荐 -->
      <hot-panel :hotList="hotList" />
      <!-- 轮播图 -->
    </template>
    <XtxGuess ref="guessRef" />
  </scroll-view>
</template>

<script setup lang="ts">
import CustomNavbar from '@/pages/index/components/CustomNavbar.vue'
import CategoryPanel from '@/pages/index/components/CategoryPanel.vue'
import HotPanel from '@/pages/index/components/HotPanel.vue'
import PageSkeleton from '@/pages/index/components/PageSkeleton.vue'
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { Banner, Category, Hot } from '@/types/home'
import { getBannerList, getCategoryList, getHotList } from '@/services/home'
import type { XtxGuessInstance } from '@/types/component'
import { useGuessLike } from '@/composables'

const bannerList = ref<Banner[]>([])
const getBanner = async () => {
  const resp = await getBannerList(1)
  bannerList.value = resp.result
}

const categoryList = ref<Category[]>([])
const getCategory = async () => {
  const { result } = await getCategoryList()
  categoryList.value = result
}

const hotList = ref<Hot[]>([])
const getHot = async () => {
  const { result } = await getHotList()
  hotList.value = result
}

const loading = ref<boolean>(true)
onLoad(async () => {
  loading.value = true
  await Promise.allSettled([getBanner(), getCategory(), getHot()])
  loading.value = false
})

const { guessRef, onScrolltolower } = useGuessLike()

const refreshing = ref<boolean>(false)
// 下拉刷新事件
const onRefresh = async () => {
  refreshing.value = true
  guessRef.value?.clearData()
  await Promise.allSettled([getBanner(), getCategory(), getHot(), guessRef.value?.getMoreData()])
  refreshing.value = false
}
</script>

<style lang="scss">
/* #ifdef APP-PLUS */
#app,
/* #endif */
  /* app端page样式无法生效 */
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
